package com.sx.pipeline.ui.goods

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.mvvmlibs.bus.RxBus
import com.sx.mvvmlibs.view.SpaceItemDecoration2
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.GoodsManageAdapter
import com.sx.pipeline.adapter.LedgerDeviceAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.event.DeviceEvent
import com.sx.pipeline.app.event.ProductEvent
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActGoodsManagerBinding
import com.sx.pipeline.repos.http.res.DeviceInfoVo
import com.sx.pipeline.repos.http.res.GoodsInfoVo
import com.sx.pipeline.ui.ledger.LedgerVM
import io.reactivex.rxjava3.schedulers.Schedulers


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 供应商-商品管理
 */
@Route(path = RoutePath.Mine.COMMODITY)
class GoodsManagerActivity :
    CYDataBindingBaseActivity<ActGoodsManagerBinding, GoodsVM>(R.layout.act_goods_manager), View.OnClickListener {
    private var key=""
    private lateinit var mAdapter: GoodsManageAdapter

    override fun initData() {
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
        mBinding.titleBar.rightView.setOnClickListener {
            RouteManager.Mine.intentToGoodsAdd(1,0)
        }
        //请求商品列表
        mViewModel.getProductList(key)

        RxBus.getDefault().toObservable(ProductEvent::class.java)
            .subscribeOn(Schedulers.io())
            .subscribe {
                //刷新设备列表
                mViewModel.page=1
                mViewModel.getProductList("")
            }
    }

    private fun initRv() {
        if (!this::mAdapter.isInitialized) {
            mAdapter = GoodsManageAdapter()
            mBinding.recyclerView.layoutManager =
                GridLayoutManager(this, 2)
            mBinding.recyclerView.addItemDecoration(
                SpaceItemDecoration2(20)
            )
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            var info = adapter.data[position] as GoodsInfoVo
            RouteManager.Mine.intentToGoodsDetail(info.id,0)
        }
        mBinding.recyclerView.adapter = mAdapter
    }


    override fun initViewObservable() {
        super.initViewObservable()

        mViewModel.deviceListEvent.observe(this) {
            initRv()
            val taskList = it as List<GoodsInfoVo>
            if (mViewModel.page == 1) {
                if (taskList.isNotEmpty()) {
                    mAdapter?.data = taskList as MutableList<GoodsInfoVo>
                    mBinding.recyclerView.visibility = View.VISIBLE
                    mBinding.emptyView.visibility = View.GONE
                } else {
                    mBinding.recyclerView.visibility = View.GONE
                    mBinding.emptyView.visibility = View.VISIBLE
                }
                mBinding.refreshLayout.finishRefresh()
            } else {
                if (taskList.isNotEmpty()) {
                    mAdapter?.addData(taskList)
                    mBinding.refreshLayout.finishLoadMore()
                } else {
                    mBinding.refreshLayout.finishLoadMoreWithNoMoreData()
                }
            }
        }

    }

    override fun initListener() {
        super.initListener()
        mBinding?.refreshLayout?.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
                mViewModel.page += 1
                mViewModel.getProductList(key)
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                mViewModel.page = 1
                mViewModel.getProductList(key)
            }
        })


        mBinding.tvAdd.setOnClickListener {
            RouteManager.Mine.intentToTaskReleaseAdd()
        }

        mBinding.etKeyword.addTextChangedListener(object : TextWatcher {
            override fun onTextChanged(
                s: CharSequence,
                start: Int,
                before: Int,
                count: Int
            ) {

            }

            override fun beforeTextChanged(
                s: CharSequence,
                start: Int,
                count: Int,
                after: Int
            ) {

            }

            override fun afterTextChanged(s: Editable) {
                key=mBinding.etKeyword.text.toString()
            }
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            //添加设备
            R.id.tv_add -> RouteManager.Mine.intentToGoodsAdd(1,0)
            //搜索
            R.id.tv_search->{
                mViewModel.page=1
                mViewModel.getProductList(key)
            }
        }
    }

}